<?php 
class News extends CI_Controller {
	public function __construct() {
		parent::__construct();
		
		$this->load->model('articlesModel');
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->model('Jobs');
	}
	
	function readall() {
		$this->load->library('Menu');
		$menu = new Menu;
		$user =& Users::getInstance();
		$data = $menu->gen();
		$data['user'] = $user;
		$data['news'] = $this->articlesModel->getAll();
		
		if ($user->level > 0) {
			$data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
			$data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);
		}
		
		$this->load->view('news-list', $data);
	}
	
	function post() {
		$user =& Users::getInstance();
		
		if ($user->level > 0) {
			if ($this->input->post('editId') > 0) {
			$this->articlesModel->update($this->input->post('editId'), $this->input->post('title'), $this->input->post('content'));
			} else {
				$this->articlesModel->insert($this->input->post('title'), $this->input->post('content'));
			}
		}
		
		redirect('/news');
	}
	
	function edit($id) {
		$user =& Users::getInstance();
		
		if ($user->level > 0) {
			$this->articlesModel->update($id, $this->input->post('title'), $this->input->post('content'));
		}
		
		redirect('/news');
	}
	
	function delete() {
		$user =& Users::getInstance();
		
		if ($user->level > 0) {
			$id = $this->uri->rsegment(3, 0);
			
			$this->articlesModel->remove($id);
		}
		
		redirect('/news');
	}
}
?>